package com.d09cnpm2.group8.map;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

import com.google.android.gms.maps.model.LatLng;

public class GMapV2Direction {
    public final static String MODE_DRIVING = "driving";
    public final static String MODE_WALKING = "walking";
    private String TAG = "--------GMAP direction--------->";

    public GMapV2Direction() {
    }

    private String getDirectionsUrl(LatLng origin, LatLng dest) {

	// Origin of route
	String str_origin = "origin=" + origin.latitude + ","
		+ origin.longitude;

	// Destination of route
	String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

	// Sensor enabled
	String sensor = "sensor=false";

	// Building the parameters to the web service
	String parameters = str_origin + "&" + str_dest + "&" + sensor;

	// Output format
	String output = "json";

	// Building the url to the web service
	String url = "https://maps.googleapis.com/maps/api/directions/"
		+ output + "?" + parameters;

	return url;
    }

    public String getJsonResult(LatLng fromPosition, LatLng toPosition) {
	try {
	    HttpClient httpClient = new DefaultHttpClient();
	    HttpContext localContext = new BasicHttpContext();
	    HttpPost httpPost = new HttpPost(getDirectionsUrl(fromPosition,
		    toPosition));
	    HttpResponse response = httpClient.execute(httpPost, localContext);
	    InputStream in = response.getEntity().getContent();
	    String s = "";
	    String line = "";

	    // Wrap a BufferedReader around the InputStream
	    BufferedReader rd = new BufferedReader(new InputStreamReader(in));

	    // Read response until the end
	    while ((line = rd.readLine()) != null) {
		s += line;
	    }
	    return s;

	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;
    }

    // --------------------get distance from start location to end location-----
    public String getDistance(String json) {

	try {
	    JSONObject responseObj = new JSONObject(json);
	    JSONArray routesArray = responseObj.getJSONArray("routes");
	    JSONObject route = routesArray.getJSONObject(0);
	    JSONArray legs = route.getJSONArray("legs");
	    JSONObject leg = legs.getJSONObject(0);
	    JSONObject distance = leg.getJSONObject("distance");
	    String dis = distance.getString("text");
	    return dis;
	} catch (JSONException e) {
	    e.printStackTrace();
	}
	return null;
    }

    // -------------------get list points steps to destination location------
    public ArrayList<LatLng> getRoutes(String json) {
	if (json == null)
	    return null;
	try {
	    JSONObject responseObj = new JSONObject(json);
	    JSONArray routesArray = responseObj.getJSONArray("routes");
	    JSONObject route = routesArray.getJSONObject(0);
	    JSONArray legs = route.getJSONArray("legs");
	    JSONObject leg = legs.getJSONObject(0);
	    ArrayList<LatLng> points = new ArrayList<LatLng>();
	
	    JSONArray steps = leg.getJSONArray("steps");
	    for (int i = 0; i < steps.length(); i++) {
		JSONObject point = steps.getJSONObject(i);
		JSONObject startLoc = point.getJSONObject("start_location");
		LatLng startLat = new LatLng(startLoc.getDouble("lat"),
			startLoc.getDouble("lng"));
		points.add(startLat);
		JSONObject endLoc = point.getJSONObject("end_location");
		LatLng endLat = new LatLng(endLoc.getDouble("lat"),
			endLoc.getDouble("lng"));
		points.add(endLat);
	    }
	    return points;
	} catch (JSONException e) {
	    e.printStackTrace();
	}
	return null;
    }

}
